/*
 * Copyright (c) 2015, the Dart project authors.
 *
 * Licensed under the Eclipse Public License v1.0 (the "License"); you may not use this file except
 * in compliance with the License. You may obtain a copy of the License at
 *
 * http://www.eclipse.org/legal/epl-v10.html
 *
 * Unless required by applicable law or agreed to in writing, software distributed under the License
 * is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express
 * or implied. See the License for the specific language governing permissions and limitations under
 * the License.
 */
package vm.element

// This file is generated by the script: pkg/vm_service/tool/generate.dart in dart-lang/sdk.

import com.google.gson.JsonElement
import com.google.gson.JsonObject

/**
 * A {@link ProfileFunction} contains profiling information about a Dart or native function.
 */
class ProfileFunction(json: JsonObject) : Element(json) {

    /**
     * The number of times function appeared on the top of the stack during sampling events.
     */
    fun getExclusiveTicks(): Int {
        return getAsInt("exclusiveTicks")
    }

    /**
     * The function captured during profiling.
     *
     * @return one of <code>FuncRef</code> or <code>NativeFunction</code>
     */
    fun getFunction(): Any? {
        val elem: JsonElement? = json.get("function")
        if (elem == null) return null

        if (elem.isJsonObject) {
            val o = elem.asJsonObject
            if (o.get("type").asString == "@Func") return FuncRef(o)
            if (o.get("type").asString == "NativeFunction") return NativeFunction(o)
        }
        return null
    }

    /**
     * The number of times function appeared on the stack during sampling events.
     */
    fun getInclusiveTicks(): Int {
        return getAsInt("inclusiveTicks")
    }

    /**
     * The kind of function this object represents.
     */
    fun getKind(): String? {
        return getAsString("kind")
    }

    /**
     * The resolved URL for the script containing function.
     */
    fun getResolvedUrl(): String? {
        return getAsString("resolvedUrl")
    }
}
